package com.lai.gulimall.product.web;

import com.lai.gulimall.product.entity.CategoryEntity;
import com.lai.gulimall.product.service.CategoryService;
import com.lai.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/","index.html"})
    public String indexPage(Model model){

        //获取1级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Category();

        model.addAttribute("categorys",categoryEntities);

        //springmvc ViewReslover视图解析器 会进行拼串
        //classpath:/templates/+ 返回值+ .html
        return "index";
    }


    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }


    @GetMapping("/hello")
    @ResponseBody
    public String hello() throws InterruptedException {
        //获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my-lock");
        //加锁   阻塞式等待，默认加的锁都是30s时间

        // Redisson  1、锁的自动续期，如果业务超长，运行期间会自动给锁续上新的30s
        //2、加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认在30s后解锁
        lock.lock();
        try{
            System.out.println("加锁成功，执行业务"+Thread.currentThread().getId());
            Thread.sleep(30000);
        }finally {
             //解锁
            System.out.println("解锁。。。。"+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

    //可以保存一定能督导最新数据，修改期间，写锁是一个排他锁（互斥锁）。读锁是一个共享锁
    //写锁没释放，读锁必须等待
    //读 + 读：相当于无锁，并发读，只会在redis中记录好
    //写 + 读：等待写锁释放
    //写 + 写：阻塞方式
    //读 + 写：有读锁，写也必须等待
    //只要有些的存在，都必须等待
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        //加写锁
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        //加读锁
        RLock rLock = lock.readLock();
        try {
            rLock.lock();
            s = redisTemplate.opsForValue().get("writeValue");
            /*Thread.sleep(30000);*/
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;
    }



}
